package org.jeecg.modules.system.announcement.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.system.announcement.entity.BikeAnnouncement;
import org.jeecg.modules.system.announcement.service.IBikeAnnouncementService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.announcement.vo.BikeAnnouncementRsp;
import org.jeecg.modules.system.announcement.vo.BikeSysMsgReq;
import org.jeecg.modules.utils.RichTextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 消息管理
 * @Author: 月月鸟
 * @Date:   2021-06-15
 * @Version: V1.0
 */
@Api(tags="消息管理")
@RestController
@RequestMapping("/announcement/bikeAnnouncement")
@Slf4j
public class BikeAnnouncementController extends JeecgController<BikeAnnouncement, IBikeAnnouncementService> {
	@Autowired
	private IBikeAnnouncementService bikeAnnouncementService;

	/**
	 * 分页列表查询
	 *
	 * @param bikeAnnouncement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="消息管理-分页列表查询", notes="消息管理-分页列表查询")
	@GetMapping(value = "/pageList1")
	public Result<?> queryPageList1(BikeAnnouncement bikeAnnouncement,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BikeAnnouncement> queryWrapper = new QueryWrapper<>();
		bikeAnnouncement.setMsgCategory("1");

		if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
			queryWrapper.eq("msg_category",bikeAnnouncement.getMsgCategory());
		}
		if (StringUtils.isNotEmpty(bikeAnnouncement.getTitile())){
			queryWrapper.like("titile",bikeAnnouncement.getTitile());
		}
		Page<BikeAnnouncement> page = new Page<BikeAnnouncement>(pageNo, pageSize);
		IPage<BikeAnnouncement> pageList = bikeAnnouncementService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param bikeAnnouncement
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="消息管理-分页列表查询", notes="消息管理-分页列表查询")
	 @GetMapping(value = "/pageList2")
	 public Result<?> queryPageList2(BikeAnnouncement bikeAnnouncement,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req) {
		 QueryWrapper<BikeAnnouncement> queryWrapper = new QueryWrapper<>();
		 bikeAnnouncement.setMsgCategory("2");

		 if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
			 queryWrapper.eq("msg_category",bikeAnnouncement.getMsgCategory());
		 }
		 if (StringUtils.isNotEmpty(bikeAnnouncement.getTitile())){
			 queryWrapper.like("titile",bikeAnnouncement.getTitile());
		 }
		 Page<BikeAnnouncement> page = new Page<BikeAnnouncement>(pageNo, pageSize);
		 IPage<BikeAnnouncement> pageList = bikeAnnouncementService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param bikeAnnouncement
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="消息管理-分页列表查询", notes="消息管理-分页列表查询")
	 @GetMapping(value = "/pageList4")
	 public Result<?> queryPageList4(BikeAnnouncement bikeAnnouncement,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req) {
		 QueryWrapper<BikeAnnouncement> queryWrapper = new QueryWrapper<>();
		 bikeAnnouncement.setMsgCategory("4");

		 if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
			 queryWrapper.eq("msg_category",bikeAnnouncement.getMsgCategory());
		 }
		 if (StringUtils.isNotEmpty(bikeAnnouncement.getTitile())){
			 queryWrapper.like("titile",bikeAnnouncement.getTitile());
		 }
		 Page<BikeAnnouncement> page = new Page<BikeAnnouncement>(pageNo, pageSize);
		 IPage<BikeAnnouncement> pageList = bikeAnnouncementService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param bikeAnnouncement
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="消息管理-分页列表查询", notes="消息管理-分页列表查询")
	 @GetMapping(value = "/pageList5")
	 public Result<?> queryPageList5(BikeAnnouncement bikeAnnouncement,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req) {
		 QueryWrapper<BikeAnnouncement> queryWrapper = new QueryWrapper<>();
		 bikeAnnouncement.setMsgCategory("5");

		 if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
			 queryWrapper.eq("msg_category",bikeAnnouncement.getMsgCategory());
		 }
		 if (StringUtils.isNotEmpty(bikeAnnouncement.getTitile())){
			 queryWrapper.like("titile",bikeAnnouncement.getTitile());
		 }
		 Page<BikeAnnouncement> page = new Page<BikeAnnouncement>(pageNo, pageSize);
		 IPage<BikeAnnouncement> pageList = bikeAnnouncementService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param bikeAnnouncement
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="消息管理-分页列表查询", notes="消息管理-分页列表查询")
	 @GetMapping(value = "/pageList6")
	 public Result<?> queryPageList6(BikeAnnouncement bikeAnnouncement,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req) {
		 QueryWrapper<BikeAnnouncement> queryWrapper = new QueryWrapper<>();
		 bikeAnnouncement.setMsgCategory("6");

		 if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
			 queryWrapper.eq("msg_category",bikeAnnouncement.getMsgCategory());
		 }
		 if (StringUtils.isNotEmpty(bikeAnnouncement.getTitile())){
			 queryWrapper.like("titile",bikeAnnouncement.getTitile());
		 }
		 Page<BikeAnnouncement> page = new Page<BikeAnnouncement>(pageNo, pageSize);
		 IPage<BikeAnnouncement> pageList = bikeAnnouncementService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  * 分页列表查询
	  *
	  * @param bikeAnnouncement
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="消息管理-分页列表查询", notes="消息管理-分页列表查询")
	 @GetMapping(value = "/pageList7")
	 public Result<?> queryPageList7(BikeAnnouncement bikeAnnouncement,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req) {
		 QueryWrapper<BikeAnnouncement> queryWrapper = new QueryWrapper<>();
		 bikeAnnouncement.setMsgCategory("7");

		 if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
			 queryWrapper.eq("msg_category",bikeAnnouncement.getMsgCategory());
		 }
		 if (StringUtils.isNotEmpty(bikeAnnouncement.getTitile())){
			 queryWrapper.like("titile",bikeAnnouncement.getTitile());
		 }
		 Page<BikeAnnouncement> page = new Page<BikeAnnouncement>(pageNo, pageSize);
		 IPage<BikeAnnouncement> pageList = bikeAnnouncementService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  * 分页列表查询
	  *
	  * @param bikeAnnouncement
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="消息管理-分页列表查询", notes="消息管理-分页列表查询")
	 @GetMapping(value = "/pageList8")
	 public Result<?> queryPageList8(BikeAnnouncement bikeAnnouncement,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req) {
		 QueryWrapper<BikeAnnouncement> queryWrapper = new QueryWrapper<>();
		 bikeAnnouncement.setMsgCategory("8");

		 if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
			 queryWrapper.eq("msg_category",bikeAnnouncement.getMsgCategory());
		 }
		 if (StringUtils.isNotEmpty(bikeAnnouncement.getTitile())){
			 queryWrapper.like("titile",bikeAnnouncement.getTitile());
		 }
		 Page<BikeAnnouncement> page = new Page<BikeAnnouncement>(pageNo, pageSize);
		 IPage<BikeAnnouncement> pageList = bikeAnnouncementService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	/**
	 *   添加
	 *
	 * @param bikeAnnouncement
	 * @return
	 */
	@AutoLog(value = "消息管理-添加")
	@ApiOperation(value="消息管理-添加", notes="消息管理-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikeAnnouncement bikeAnnouncement) {
		if (bikeAnnouncement.getSendStatus().equals(BikeConstant.y_n_yes)){
			bikeAnnouncement.setSendTime(new Date());
		}
		if (!bikeAnnouncement.getMsgCategory().equals("7")){
			if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgCategory())){
				bikeAnnouncement.setMsgContent(RichTextUtils.newRichTextAddWH(bikeAnnouncement.getMsgContent()));
			}
		}
		//校验滚屏公告 最多发布3条
		if (bikeAnnouncement.getMsgCategory().equals("7")&&bikeAnnouncement.getSendStatus().equals("1")){
			String YN = bikeAnnouncementService.addCheckByType();
			if (!YN.equals(BikeConstant.y_n_yes)){
				return Result.error(YN);
			}
		}
		bikeAnnouncement.setUpdateTime(new Date());
		//bikeAnnouncement.setMsgContent(bikeAnnouncement.getMsgContent());
		bikeAnnouncementService.save(bikeAnnouncement);
		return Result.OK("添加成功！");
	}

	 /**
	  *  消息管理-发布/撤回
	  *  send_status 0 未发布 1 发布 2 撤回
	  *
	  * @param id  消息id
	  * @return
	  */
	 @AutoLog(value = "消息管理-发布/撤回",operateType = 3)
	 @ApiOperation(value="消息管理-发布/撤回", notes="消息管理-发布/撤回")
	 @PutMapping(value = "/releaseOrRevoke")
	 public Result<?> releaseOrRevoke(@RequestParam String id) {
		 return bikeAnnouncementService.releaseOrRevoke(id);
	 }

	/**
	 *  编辑
	 *
	 * @param bikeAnnouncement
	 * @return
	 */
	@AutoLog(value = "消息管理-编辑",operateType = 3)
	@ApiOperation(value="消息管理-编辑", notes="消息管理-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikeAnnouncement bikeAnnouncement) {
		if (bikeAnnouncement.getSendStatus().equals(BikeConstant.y_n_yes)){
			bikeAnnouncement.setSendTime(new Date());
		}
		if (StringUtils.isNotEmpty(bikeAnnouncement.getMsgContent())){
			bikeAnnouncement.setMsgContent(RichTextUtils.newRichTextAddWH(bikeAnnouncement.getMsgContent()));
		}
		//校验滚屏公告 最多发布3条
		if (bikeAnnouncement.getMsgCategory().equals("7")){

			String YN = bikeAnnouncementService.checkCountByType(bikeAnnouncement.getId());
			if (!YN.equals(BikeConstant.y_n_yes)){
				return Result.error(YN);
			}
		}
		//bikeAnnouncement.setMsgContent(bikeAnnouncement.getMsgContent());
		bikeAnnouncementService.updateById(bikeAnnouncement);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "消息管理-通过id删除")
	@ApiOperation(value="消息管理-通过id删除", notes="消息管理-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		return bikeAnnouncementService.removeNewsById(id);
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "消息管理-批量删除")
	@ApiOperation(value="消息管理-批量删除", notes="消息管理-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.bikeAnnouncementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="消息管理-通过id查询", notes="消息管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BikeAnnouncement bikeAnnouncement = bikeAnnouncementService.getById(id);
		if(bikeAnnouncement==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bikeAnnouncement);
	}

	@ApiOperation(value = "消息管理-后台系统消息提醒")
	@GetMapping(value ="/getSysMsgByUser")
	public Result<?> getSysMsgByUser(BikeSysMsgReq bikeSysMsgReq,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req){
		Page<BikeAnnouncementRsp> page = new Page<>(pageNo, pageSize);
		List<BikeAnnouncementRsp> sysMsgByUser = bikeAnnouncementService.getSysMsgByUser(bikeSysMsgReq);
		page.setRecords(sysMsgByUser);
		page.setTotal(sysMsgByUser.size());
		return Result.OK(page);
	}
	 @ApiOperation(value = "消息管理-后台系统消息已读")
	 @PutMapping(value = "/readMsgById")
	 public Result<?> readMsgById(@RequestBody BikeSysMsgReq bikeSysMsgReq) {
		 if (StringUtils.isEmpty(bikeSysMsgReq.getId()))
			 return Result.error("未选择消息");
		 return bikeAnnouncementService.readMsgById(bikeSysMsgReq.getId());
	 }

	 @ApiOperation(value = "消息管理-后台系统消息未读数量")
	 @GetMapping(value = "/getSysMsgNoReadCount")
	 public Result<?> getSysMsgNoReadCount() {
		 return bikeAnnouncementService.getSysMsgNoReadCount();
	 }

	 @ApiOperation(value = "消息管理-测试web后台消息")
	 @PostMapping(value = "/test")
	 public Result<?> addTest(String type, String sender) {
		 String a = null;
		 String b = null;
		 if (type.equals("1")) {
			 a = BikeConstant.sys_msg_appeal;
			 b = BikeConstant.sys_msg_appeal_text;
		 } else if (type.equals("2")) {
			 a = BikeConstant.sys_msg_opinion;
			 b = BikeConstant.sys_msg_opinion_text;
		 } else if (type.equals("3")) {
			 a = BikeConstant.sys_msg_report;
			 b = BikeConstant.sys_msg_report_text;
		 }
		 List<String> sysUserIds = bikeAnnouncementService.getSysUserIds(a);
		 bikeAnnouncementService.addSysMsgByUser(sysUserIds, b, sender,a,null);
		 return Result.OK();
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param bikeAnnouncement
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BikeAnnouncement bikeAnnouncement) {
        return super.exportXls(request, bikeAnnouncement, BikeAnnouncement.class, "消息管理");
    }
    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BikeAnnouncement.class);
    }

}
